#include <bits/stdc++.h>
using namespace std;

struct Student {
    string name;
    string id;
    string course;
    double score;
};

vector<Student> records;
map<string, Student> idMap; // 用于学号重复检查

void showMainMenu();
void addRecord();
void queryRecord();
void statistics();
//主函数
int main() {
    int choice;
    do {
        showMainMenu();
        cin >> choice;
        switch(choice) {
            case 1:
                addRecord();
                break;
            case 2:
                queryRecord();
                break;
            case 3:
                statistics();
                break;
            case 4:
                cout << "\n感谢使用学生成绩管理系统，再见！" << endl;
                break;
            default:
                cout << "无效的选项，请重新输入！" << endl;
        }
    } while(choice != 4);

    return 0;
}
//展示学生成绩管理系统菜单
void showMainMenu() {
    cout << "=================================\n"
         << "  欢迎使用学生成绩管理系统\n"
         << "=================================\n"
         << "请选择操作：\n"
         << "1. 记录学生成绩\n"
         << "2. 查询学生成绩\n"
         << "3. 统计课程成绩\n"
         << "4. 退出系统\n\n"
         << "请输入选项序号：";
}

void addRecord() {
    Student s;
    cout << "===== 记录学生成绩 =====\n";
    while(true) {
        cout << "请输入学生姓名：";
        cin>>s.name;
        if(!s.name.empty()) break;
        cout << "姓名不能为空！\n";
    }

    while(true) {
        cout << "请输入学生学号：";
        cin>>s.id;
        if(idMap.find(s.id) == idMap.end()) break;
        cout << "学号已存在，请重新输入！\n";
    }

    while(true) {
        cout << "请输入课程名称：";
        cin>>s.course;
        if(!s.course.empty()) break;
        cout << "课程名称不能为空！\n";
    }

    while(true) {
        cout << "请输入成绩（0-100）：";
        cin >> s.score;
        if(cin.fail() || s.score < 0 || s.score > 100) {
            cin.clear();
            cout << "无效的成绩输入，请重新输入！\n";
        } else {
            break;
        }
    }
    records.push_back(s);
    idMap[s.id] = s;
    cout << "\n成绩已成功记录！\n";
}
//查询成绩
void queryRecord() {
    int choice;
    cout << "===== 查询学生成绩 =====\n"
         << "请选择查询方式：\n"
         << "1. 按学生姓名查询\n"
         << "2. 按学生学号查询\n"
         << "3. 按课程名称查询\n"
         << "请输入选项序号：";
    cin >> choice;

    switch (choice) {
        case 1: { // 按姓名查询
            string name;
            cout << "请输入学生姓名：";
            cin>>name;
            bool found = false;
            for (const auto& record : records) {
                if (record.name == name) {
                    cout << "姓名：" << record.name
                         << ", 学号：" << record.id
                         << ", 课程：" << record.course
                         << ", 成绩：" << record.score << endl;
                    found = true;
                }
            }
            if (!found) {
                cout << "未找到该学生的成绩记录！" << endl;
            }
            break;
        }
        case 2: { // 按学号查询
            string id;
            cout << "请输入学生学号：";
            cin>>id;
            auto it = idMap.find(id);
            if (it != idMap.end()) {
                cout << "姓名：" << it->second.name
                     << ", 学号：" << it->second.id
                     << ", 课程：" << it->second.course
                     << ", 成绩：" << it->second.score << endl;
            } else {
                cout << "未找到该学号的成绩记录！" << endl;
            }
            break;
        }
        case 3: { // 按课程名称查询
            string course;
            cout << "请输入课程名称：";
            cin>>course;
            bool found = false;
            for (const auto& record : records) {
                if (record.course == course) {
                    cout << "姓名：" << record.name
                         << ", 学号：" << record.id
                         << ", 课程：" << record.course
                         << ", 成绩：" << record.score << endl;
                    found = true;
                }
            }
            if (!found) {
                cout << "未找到该课程的成绩记录！" << endl;
            }
            break;
        }
        default:
            cout << "无效的选项！" << endl;
    }
}

void statistics() {
    cout << "===== 统计课程成绩 =====\n";
    string course;
    cout << "请输入课程名称：";
    cin>>course;
    double totalScore = 0;
    int count = 0;
    double maxScore = -1;
    double minScore = 101;

    for (const auto& record : records) {
        if (record.course == course) {
            totalScore += record.score;
            count++;
            if (record.score > maxScore) {
                maxScore = record.score;
            }
            if (record.score < minScore) {
                minScore = record.score;
            }
        }
    }

    if (count == 0) {
        cout << "未找到该课程的成绩记录！" << endl;
    } else {
        double averageScore = totalScore / count;
        cout << "课程：" << course << endl;
        cout << "平均分：" << fixed << setprecision(2) << averageScore << endl;
        cout << "最高分：" << maxScore << endl;
        cout << "最低分：" << minScore << endl;
    }
}